'use strict';

angular.module('snPaas')
.directive('resourceGauge',['baseUrl','$interval',function(baseUrl,$timeout){
    return {
      restrict:'A',
      priority:-1,
      templateUrl :baseUrl.static+'template/directive/resourceGauge.html',
      replace:true,
      scope:{
         rgTitle:'=rgTitle',
         rgData:'=rgData',
         rgStyle:'=rgStyle'
      },
      // controller:function(){},
      link:function postLink(scope,iElement,iAttrs){
         scope.percent = parseInt(scope.rgData.used*100/scope.rgData.total);
         var deg = 278*scope.percent/100 +42;
         scope.resourceImg = {'background-image':'url('+baseUrl.static+scope.rgStyle.img+')'};
         scope.color = {'background-color':scope.rgStyle.color};
         var time = 1000;

         $timeout(function(){
            if(deg>180){
               time = scope.percent*17;
               iElement.find('.gauge-bar2').css({
                  '-webkit-transition': 'all '+time+'ms ease-in-out',
                  '-ms-transition': 'all '+time+'ms ease-in-out',
                  '-moz-transition': 'all '+time+'ms ease-in-out',
                  '-o-transition': 'all '+time+'ms ease-in-out',
                  'transition': 'all '+time+'ms ease-in-out'
               });
               iElement.find('.gauge-bar').css({
                  'transform':'rotate(180deg)',
                  '-ms-transform':'rotate(180deg)',   
                  '-moz-transform':'rotate(180deg)',  
                  '-webkit-transform':'rotate(180deg)',
                  '-o-transform':'rotate(180deg)'
               });
               iElement.find('.gauge-bar2').css({
                  'transform':'rotate('+deg+'deg)',
                  '-ms-transform':'rotate('+deg+'deg)',   
                  '-moz-transform':'rotate('+deg+'deg)',  
                  '-webkit-transform':'rotate('+deg+'deg)',
                  '-o-transform':'rotate('+deg+'deg)'
               });

            }
            else{
               iElement.find('.gauge-bar,.gauge-bar2').css({
                  'transform':'rotate('+deg+'deg)',
                  '-ms-transform':'rotate('+deg+'deg)',   
                  '-moz-transform':'rotate('+deg+'deg)',  
                  '-webkit-transform':'rotate('+deg+'deg)',
                  '-o-transform':'rotate('+deg+'deg)'
               });
            }
            

         },0);
       $timeout(function(){
         if(deg>180){
            iElement.find('.gauge-bkg.bkg-clip').css({
               'z-index':'14'
            });
         }
      },800);
       $timeout(function(){
         var h = (deg/360)*Math.PI*2;
         var x = 100 - Math.sin(h)*80 -26;
         var y = 100 + Math.cos(h)*80 -20;
         iElement.find('.gauge-percent-text').css({
            'top': y+'px',
            'left': x+'px',
            'opacity': 1
         });

       },time);


      }
   }

}]);
